package org.agent.action;

import java.util.List;

import org.agent.common.Constants;
import org.agent.pojo.SystemConfig;
import org.agent.service.systemconfig.SystemConfigService;
import org.apache.log4j.Logger;

public class SystemConfigAction extends BaseAction {
	
	private Logger logger = Logger.getLogger(SystemConfigAction.class);
	//由struts2-spring-plugun插件完成属性装配
	private SystemConfigService systemConfigService;
	
	private Integer configType;//当前配置项类型
	private String configTypeName;//
	private SystemConfig systemConfig;
	private List<SystemConfig> systemConfigList;//多个二级编号
	private List<SystemConfig> systemConfigIsStartList;
	
/**
 * 根据一级编号，查询
 * */
	private void loadConfig(Integer confType,String configTypeName,String info){
		logger.info(info);
		SystemConfig _sysSystemConfig = new SystemConfig();
		_sysSystemConfig.setConfigType(confType);
		this.systemConfigList =	systemConfigService.getSystemConfigs(_sysSystemConfig);
		_sysSystemConfig.setIsStart(1);//只查启用状态的
		this.systemConfigIsStartList=systemConfigService.getSystemConfigsIsStart(_sysSystemConfig);
		this.configType=confType;//设置上下文中的configType
		this.configTypeName=configTypeName;
		//this.systemConfig=systemConfigService.
		if(this.systemConfigList!=null&&this.systemConfigList.size()==1){
			//如果某一配置项有多个子配置的话，systemConfiglist无效,
			//如果某一项只有1个值时取list第一个值
			this.systemConfig=this.systemConfigList.get(0);
		}
		//保持constans里的配置项为最新
		Constants.configSystemDate();
	
	}
	public String financeType() {
		loadConfig(1, "财务类型","查看财务类型配置信息");
		return SUCCESS;
	}

	public String serviceType() {
		loadConfig(2, "服务类型","查看服务类型配置信息");
		return SUCCESS;
	}

	public String serviceYears() {
		loadConfig(3, "服务年限","查看服务年限配置信息");
		return SUCCESS;
	}

	public String appurl() {
		loadConfig(4, "app地址","查看app地址配置信息");
		return SUCCESS;
	}

	public String customType() {
		loadConfig(5,"客户类型", "查看客户类型配置信息");
		return SUCCESS;
	}

	public String cardType() {
		loadConfig(6, "证件类型","查看证件类型配置信息");
		return SUCCESS;
	}

	public String sellType() {
		loadConfig(7,"优惠信息", "查看优惠配置信息");
		return SUCCESS;
	}
	
	//用ajax请求   添加
	public void addConfig(){
		logger.debug("-------------进入addConfig-------------");
		if(this.systemConfigService.isPeatConfig(systemConfig)>0){
			this.getOut().print("peat");
			logger.debug("-------------添加的类型重复-------------");
		}else{
			Integer maxConfigTypeValue=this.systemConfigService.maxTypeValueType(this.systemConfig.getConfigType());
			this.systemConfig.setConfigTypeValue(maxConfigTypeValue+1);
			this.systemConfigService.addSystemConfig(this.systemConfig);
			this.getOut().print("nopeat");
			logger.debug("-------------添加类型成功-------------");
		}
	}
	//用ajax请求   删除
	public void deleteConfig(){
		logger.debug("-------------进入deleteConfig-------------");
		if(this.systemConfigService.deleteSystemConfig(systemConfig)>0){
			this.getOut().print("success");
		}else{
			this.getOut().print("faild");
		}
		
		logger.debug("-------------删除成功-------------");
		}
	//用ajax请求   修改
	public void modifyConfig(){
		logger.debug("-------------进入modifyConfig-------------");
		System.out.println(systemConfig.toString()+"++++++++++++");
			//只修改configTypeName和isStart，其他不能修改
			if(this.systemConfigService.modifySystemConfig(systemConfig)>0){
				this.getOut().print("success");
				logger.debug("-------------修改类型成功-------------");
			}else{
				this.getOut().print("faild");
			}
			
		
	}
	
	
//set and get
	public SystemConfigService getSystemConfigService() {
		return systemConfigService;
	}

	public void setSystemConfigService(SystemConfigService systemConfigService) {
		this.systemConfigService = systemConfigService;
	}

	public Integer getConfigType() {
		return configType;
	}

	public void setConfigType(Integer configType) {
		this.configType = configType;
	}

	public SystemConfig getSystemConfig() {
		return systemConfig;
	}

	public void setSystemConfig(SystemConfig systemConfig) {
		this.systemConfig = systemConfig;
	}

	public List<SystemConfig> getSystemConfigList() {
		return systemConfigList;
	}

	public void setSystemConfigList(List<SystemConfig> systemConfigList) {
		this.systemConfigList = systemConfigList;
	}

	public List<SystemConfig> getSystemConfigIsStartList() {
		return systemConfigIsStartList;
	}

	public void setSystemConfigIsStartList(
			List<SystemConfig> systemConfigIsStartList) {
		this.systemConfigIsStartList = systemConfigIsStartList;
	}
	public String getConfigTypeName() {
		return configTypeName;
	}
	public void setConfigTypeName(String configTypeName) {
		this.configTypeName = configTypeName;
	}
	
	
}
